package com.xiaoma.myaudience.biz.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class VideoDetailInfo extends BaseVideoInfo {
	
    /**视频ID*/
    public static final String PARAM_PID = "pid";
    /**地区*/
    public static final String PARAM_AREA = "area";
    /**影片集数*/
    public static final String PARAM_COUNTS = "counts";
    /**影片简介*/
    public static final String PARAM_DIGEST = "digest";
    /**影片上映年份*/
    public static final String PARAM_LAUNCH = "launch";
    /**影片主演*/
    public static final String PARAM_PERFORMER = "performer";
    /**导演*/
    public static final String PARAM_DIRECTOR = "director";
    /**跟贴版块*/
    public static final String PARAM_REPLYBOARD = "replyboard";
    /**跟贴ID*/
    public static final String PARAM_REPLYID = "replyid";
    /**评分*/
    public static final String PARAM_SCORE = "score";
    /**影片类型 film|tv|doc|etc*/
    public static final String PARAM_VTYPE = "vtype";
    /**剧情类型：家庭 都市 爱情*/
    public static final String PARAM_TYPE = "type";
    /**==========================影片列表==========================================*/
    /**影片列表*/
    public static final String PARAM_PLAYLIST = "playlist";
    /**影片列表——index*/
    public static final String PARAM_INDEX = "index";
    /**影片列表——剧集ID*/
    public static final String PARAM_VID = "vid";
    /**影片列表——剧集高清URL*/
    public static final String PARAM_MP4HDVURL = "mp4_hd";
    /**影片列表——剧集普请URL*/
    public static final String PARAM_MP4SDVURL = "mp4_sd";
    
    public String mPid;
    public String mArea;
    public String mCounts;
    public String mDigest;
    public String mLaunchTime;
    public String mPerformer;
    public String mDirector;
    public String mReplyboard;
    public String mReplyid;
    public String mScore;
    public String mVtype;
    public String mType;
    public List<VideoListInfo> mPlaylist;
    
    //PARAM_VID PARAM_TITLE PARAM_IMG
    /***
     * 从JSONObject中读取到Info中
     */
    @Override
    public void readFromJSONObject(JSONObject o) throws JSONException{
        super.readFromJSONObject(o);
        if (o != null) {

        	if(o.has(PARAM_PID)) mPid = o.getString(PARAM_PID);
        	if(o.has(PARAM_AREA)) mArea = o.getString(PARAM_AREA);
        	if(o.has(PARAM_COUNTS)) mCounts = o.getString(PARAM_COUNTS);
        	if(o.has(PARAM_DIGEST)) mDigest = o.getString(PARAM_DIGEST);
        	if(o.has(PARAM_LAUNCH)) mLaunchTime = o.getString(PARAM_LAUNCH);
        	if(o.has(PARAM_PERFORMER)) mPerformer = o.getString(PARAM_PERFORMER);
        	if(o.has(PARAM_DIRECTOR)) mDirector = o.getString(PARAM_DIRECTOR);
        	if(o.has(PARAM_REPLYBOARD)) mReplyboard = o.getString(PARAM_REPLYBOARD);
        	if(o.has(PARAM_REPLYID)) mReplyid = o.getString(PARAM_REPLYID);
        	if(o.has(PARAM_SCORE)) mScore = o.getString(PARAM_SCORE);
        	if(o.has(PARAM_VTYPE)) mVtype = o.getString(PARAM_VTYPE);
        	if(o.has(PARAM_TYPE)) mType = o.getString(PARAM_TYPE);
        	
        	 /**==========================影片列表==========================================*/
        	if(o.has(PARAM_PLAYLIST)){
        		mPlaylist = new ArrayList<VideoListInfo>();
        		
        		JSONArray videolist = o.getJSONArray(PARAM_PLAYLIST);
        		
        		if(null != videolist){
        			
        			int len = videolist.length();
        			
        			for(int i = 0; i < len; i ++){

        				VideoListInfo vlinfo = new VideoListInfo();

        				JSONObject indexObj = videolist.getJSONObject(i);
        				if(indexObj.has(PARAM_INDEX)) vlinfo.mIndex = indexObj.getString(PARAM_INDEX);
        				if(indexObj.has(PARAM_VID)) vlinfo.mVid = indexObj.getString(PARAM_VID);
        				if(indexObj.has(PARAM_MP4HDVURL)) vlinfo.mMp4HdUrl = indexObj.getString(PARAM_MP4HDVURL);
        				if(indexObj.has(PARAM_MP4SDVURL)) vlinfo.mMp4SdUrl = indexObj.getString(PARAM_MP4SDVURL);
            			
        				mPlaylist.add(vlinfo);
        			}
        			
        		}
        	}
        }
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int paramInt) {
        super.writeToParcel(parcel, paramInt);
       
        parcel.writeString(mPid);
        parcel.writeString(mArea);
        parcel.writeString(mCounts);
        parcel.writeString(mDigest);
        parcel.writeString(mLaunchTime);
        parcel.writeString(mPerformer);
        parcel.writeString(mDirector);
        parcel.writeString(mReplyboard);
        parcel.writeString(mReplyid);
        parcel.writeString(mScore);
        parcel.writeString(mVtype);
        parcel.writeString(mType);
        parcel.writeList(mPlaylist);
    }  
    
    @SuppressWarnings("unchecked")
	@Override
    public void readFromParcel(Parcel source) {
        super.readFromParcel(source);
        
        mPid = source.readString();
        mArea = source.readString();
        mCounts = source.readString();
        mDigest = source.readString();
        mLaunchTime = source.readString();
        mPerformer = source.readString();
        mDirector = source.readString();
        mReplyboard = source.readString();
        mReplyid = source.readString();
        mScore = source.readString();
        mVtype = source.readString();
        mType = source.readString();
           
        ClassLoader cl = VideoDetailInfo.VideoListInfo.class.getClassLoader();
        mPlaylist = source.readArrayList(cl);
        
    }

    public static final Parcelable.Creator<VideoDetailInfo> CREATOR = new Creator<VideoDetailInfo>() {
        public VideoDetailInfo createFromParcel(Parcel source) {
        	VideoDetailInfo info = new VideoDetailInfo();
            info.readFromParcel(source);
            return info;
        }

        public VideoDetailInfo[] newArray(int size) {
            return new VideoDetailInfo[size];
        }
    };
    
    public class VideoListInfo{
    	public String mIndex;
    	public String mVid;
    	public String mMp4HdUrl;
    	public String mMp4SdUrl;
    }
}
